package Question19;

import java.util.Scanner;

/**
 * Created by lenovo on 2017/3/16.
 */
public class Question19 {

    /**
     * 将字符串折叠分割
     * @param str
     * @return
     */
    private static String[] zhedie(String str){
        int len = str.length();
        String[] result = new String[len/6+1];

        if(len<6){
            result[0] = str;
        }else{
            int i ;
            for(i=0;i<len/6;i++){
                result[i] = str.substring(i*6,(i+1) * 6);
            }

            if(len%6>0){
                result[i] = str.substring(i*6,i*6+len%6);
            }
        }

        return result;
    }


    /**
     * 字符矩阵转化为数字矩阵
     * @param result
     * @return
     */
    private static Integer[][] changeToInteger(String[] result){
        Integer[][] cresult = new Integer[result.length][6];

        //初始化
        for(int i=0;i<result.length;i++){
        	for(int j =0;j<6;j++){
            	cresult[i][j] =0;
            }
        }
        
        
        for(int i=0;i<result.length;i++){
            char[] ch = result[i].toCharArray();
            for(int j =0;j<ch.length;j++){
            	cresult[i][j] =(int)(ch[j]-'0')+48;
            }
        }
        
        return cresult;
    }
    
    //相加函数
    private static Integer[] add(Integer[][] changeToInteger){
    	
    	Integer[] result = new Integer[6];
    	
    	for(int i=0;i<6;i++){
    		result[i] = 0;
    	}
    	
    	for(int i=0;i<6;i++){
    		for(int j = 0;j<changeToInteger.length;j++){
    			result[i] += changeToInteger[j][i];
    		}
    	}
    	
    	return result;
    }
    
    
    private static int cal(int num){
    	for(;;){
    		int sum = 0;
    		while(num>10){
    			sum+=num%10;
    			num=num/10;
    		}
    		sum+=num;
    		if(sum<10){
    			return sum;
    		}
    		num = sum;
    	}
    	 
    }
    public static void main(String[] args){
          Scanner scanner = new Scanner(System.in);
          int n = scanner.nextInt();
          scanner.nextLine();
          while(n-->0){
        	  Integer[] result = add(changeToInteger(zhedie(scanner.nextLine())));
        	  String str= "";
        	  for(int i=0;i<result.length;i++){
        		  str+=cal(result[i]);
        	  }
        	  System.out.println(str);
          }
    }
}
